import { loginByUsername, logout, getUserInfo,getMenu } from '@/api/login'
import { getToken, setToken, removeToken,getRefreshToken,setRefreshToken } from '@/utils/auth'
import {deptList} from '@/api/dept/dept'
const _import = require('@/router/_import_' + process.env.NODE_ENV)
import {constantRouterMap } from '@/router'
import Layout from '@/views/layout/Layout'
import router from '@/router'
import store from "@/store";

/**
 * 递归过滤异步路由表，返回符合用户角色权限的路由表
 * @param asyncRouterMap
 * @param roles
 */

function hasChild(data){
  if(data.children){
    data.path=data.name
    data.children.forEach(function(value, index, array) {
      if(value.component!=undefined&&!value.children&&value.status==1){
        value.component
        value.component= _import(value.component)
      }
      else {
        value.component= _import('layout/components/AppMain')//vue router 3级路由有问题。
      }
      hasChild(value)
    }
    );
  }
}

function toRouter(data){
  data.forEach(function(value, index, array) {
    value.component=Layout;//第一层菜单加载LAYOUT
    hasChild(value)
  })
  return data
}

const user = {
  state: {
    token: getToken(),
    refresh_token:getRefreshToken(),
    routers: constantRouterMap,
    addRouters: [],
    menuInit:true,
    deptData:[],
    access_url:[]
  },
  mutations: {
    SET_ROUTERS: (state, routers) => {
      state.addRouters = routers
      state.routers = constantRouterMap.concat(routers)
    },
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_REFRESH_TOKEN: (state, token) => {
      state.refresh_token = token
    },
    SET_MENU_INIT: (state, menuInit) => {
      state.menuInit = menuInit
    },
    SET_DEPT_DATA:(state,deptData)=>{
      state.deptData =deptData
    },
    SET_ACCESS_URL:(state,access_url)=>{
      state.access_url=access_url
    }
  },
  actions: {
    getDeptData({ commit },deptData){
      return new Promise((resolve, reject) => {
        deptList(deptData).then(response => {
          const data = response.data.data
          commit('SET_DEPT_DATA', data)
          // console.log('~~~~')
          resolve()

        }).catch(error => {
          reject(error)
        })
      })
    },

    LoginByUsername({ commit }, userInfo) {
      const username = userInfo.username.trim()
      return new Promise((resolve, reject) => {
        loginByUsername(username, userInfo.password,userInfo.imgCode).then(response => {
          let data = response.data
          commit('SET_TOKEN', data.access_token)
          commit('SET_REFRESH_TOKEN',data.refresh_token)
          setRefreshToken(data.refresh_token)
          commit('SET_ACCESS_URL',data.access)
          console.log(data.refresh_token)
          setToken(data.access_token)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    getMenu({ commit }) {
      return new Promise((resolve, reject) => {
        getMenu().then(response => {
          let data = response.data.data
          data=toRouter(data)
          // console.log(data)
          commit('SET_ROUTERS',data)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROUTERS', [])
          removeToken()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
  }
}

export default user
